﻿using System.Web.Security;
using System;
using System.Collections;
using DataAccessLogic;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace BusinessLogic
{
    public class AppUser : Table            
    {
        private Constituent _constituent;        
        private MembershipUser _membershipUser;
        private Guid _membershipUserID;
        
        public Constituent Constituent
        {
            get { return _constituent; }
            set { _constituent = value; }
        }
        
        public override void Create()
        {
            List<SqlParameter> parms = new List<SqlParameter>();
            SqlParameter p = new SqlParameter();
            p.ParameterName = "@ID";
            p.DbType = System.Data.DbType.Guid;
            p.Direction = System.Data.ParameterDirection.Output;
            parms.Add(p);

            parms.Add(new SqlParameter("@ASPNETDBUSERID", ASPNetDbUserID));
            parms.Add(new SqlParameter("@USERID", Membership.GetUser().ProviderUserKey.ToString()));
            parms.Add(new SqlParameter("@CONSTITUENTID", ConstituentID));
            base.Create(parms);
        }

        public override void Delete()
        {
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@ID", this.ID));
            base.Delete(parms);
        }

        [SQLParameter(ParameterName = "@CONSTITUENTID", SQLDoWhat = SQLDoWhatEnum.Create)]
        [SQLParameter(ColumnName = "CONSTITUENTID", SQLDoWhat = SQLDoWhatEnum.Get)]
        public Guid ConstituentID
        {
            get { return _constituent.ID; } 
            set 
            { 
                if(_constituent == null)
                {
                    _constituent = new Constituent(value);
                }
                else
                {
                    _constituent.ID = value;
                }
            }           
        }

        public MembershipUser MembershipUser
        {
            get { return _membershipUser; }
            set 
            { 
                _membershipUser = value; 
                _membershipUserID = new Guid(_membershipUser.ProviderUserKey.ToString());
            }            
        }

        [SQLParameter(ColumnName = "MEMBERSHIPUSERID", SQLDoWhat = SQLDoWhatEnum.Get)]
        public Guid MembershipUserID
        {
            get { return _membershipUserID; }
            set { _membershipUserID = value; }
        }
        
        [SQLParameter(ParameterName = "@ASPNETDBUSERID", SQLDoWhat = SQLDoWhatEnum.Create)]        
        public Guid ASPNetDbUserID
        {
            get 
            { 
                if(null != _membershipUser)
                {
                    return new Guid(_membershipUser.ProviderUserKey.ToString()); 
                }
                else
                {
                    return _membershipUserID;
                }
            }
        }

        public static List<AppUser> GetActive()
        {
            return Get(DataAccess.AppUser_DA.GetActive());
        }

        public static List<AppUser> GetAll()
        {
            return Get(DataAccess.AppUser_DA.GetAll());
        }

        private static List<AppUser> Get(List<AppUser> appUsers)
        {
            MembershipUserCollection users = Membership.GetAllUsers();

            foreach (AppUser au in appUsers)
            {
                foreach (MembershipUser mu in users)
                {
                    if (au.MembershipUserID == new Guid(mu.ProviderUserKey.ToString()))
                    {
                        au.MembershipUser = mu;
                    }
                }
            }

            return appUsers;
        }

        public override DatabaseInteractionDescriptor TableInteractionDescription()
        {
            DatabaseInteractionDescriptor desc = new DatabaseInteractionDescriptor();            
            desc.CreateStoredProcedure = StoredProcedures.USP_APPUSER_CREATE; 
            desc.DeleteStoredProcedure = StoredProcedures.USP_APPUSER_DELETE;                       
            return desc;
        }
    }
}